<template>
    <div class="basic-layout">
        <div :class="['nav',isCollapse?'fold':'unfold']">
            <div class="logo">
                <img src="../assets/logo.png" />
                <span>通用管理系统</span>
            </div>

            <!--
            default-active:
                可以激活(高亮显示)当前用户访问的路由地址对应的菜单
                可以将当前路由对应的父级路由展开
                需要开启路由配置(需要配置 :router="true")
            -->
            <el-menu class="nav-menu" background-color="#001529" text-color="#fff" :router="true" :collapse="isCollapse"
                :default-active="activeMenu">
                <tree-menu :userMenu="menuTree"></tree-menu>
            </el-menu>
        </div>

        <div :class="['content',isCollapse?'fold':'unfold']">
            <div class="top-crumb">
                <div class="top-crumb-left">
                    <div class="fold-menu" @click="toggleNav">
                        <el-icon>
                            <Operation />
                        </el-icon>
                    </div>
                    <div class="bread">
                        <bread-crumb></bread-crumb>
                    </div>
                </div>
                <div class="user-info">
                    <el-badge :is-dot=true class="notice" type="danger">
                        <el-icon>
                            <Bell />
                        </el-icon>
                    </el-badge>
                    <el-dropdown @command="handleCommand">
                        <span class="username-span">{{userInfo.userName}}</span>
                        <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item command="user">个人中心</el-dropdown-item>
                                <el-dropdown-item command="updatePwd">修改密码</el-dropdown-item>
                                <el-dropdown-item command="loginOut">退出登录</el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>
                </div>
            </div>
            <div class="wrapper">
                <router-view></router-view>
            </div>
        </div>
    </div>
</template>

<script setup>
    import TreeMenu from './TreeMenu.vue'
    import BreadCrumb from './Breadcrumb.vue'

    import {
        computed,
        inject,
        onMounted,
        ref
    } from 'vue';

    let isCollapse = ref(false);
    const toggleNav = () => {
        isCollapse.value = !isCollapse.value
    }

    import {
        useRoute,
        useRouter
    } from 'vue-router'
    import {
        useStore
    } from 'vuex'
    import storage from '../utils/storage';
    const router = useRouter()
    const store = useStore()

    const handleCommand = (key) => {
        if (key === "user") {

        } else if (key === "updatePwd") {

        } else if (key === "loginOut") {
            store.commit("clearUserInfo") //调用vuex中的clearUserInfo函数清空本地存储
            router.push("/login") //路由跳转到/login
        }
    }

    let menuTree = ref([])
    let $api = inject("$api");
    onMounted(() => {
        $api.getUserInfo().then(res => {
            //获得用户的路由信息
            menuTree.value = res.allRouters
            //获得用户的按钮权限,将其保存到本地存储中
            storage.setItem('buttonPerms', res.buttonPerms)
        })
    })

    //获得登录的用户的信息
    let userInfo = ref(store.state.userInfo)

    const route = useRoute()
    //使用activeMenu计算属性获得当前访问的路由地址
    const activeMenu = computed(() => route.path)
</script>


<style lang="scss" scoped>
    .basic-layout {
        position: relative;

        .nav {
            position: fixed;
            width: 200px;
            height: 100vh;
            background-color: #001529;
            color: white;
            overflow-y: auto;
            overflow-x: hidden;
            transition: width 0.3s; // 收缩展开过渡动画


            .logo {
                display: flex;
                align-items: center;
                font-size: 16px;
                height: 50px;


                img {
                    margin: 0 16px;
                    width: 32px;
                    height: 32px;
                }
            }


            .nav-menu {
                height: calc(100vh - 50px);
                border-right: none;
            }


            &.fold {
                width: 64px;
            }

            &.unfold {
                width: 200px;
            }
        }

        .content {
            margin-left: 200px;

            &.fold {
                margin-left: 64px;
            }

            &.unfold {
                margin-left: 200px;
            }

            .top-crumb {
                height: 50px;
                line-height: 50px;
                display: flex;
                justify-content: space-between;
                border-bottom: 1px solid #ddd;
                padding: 0 20px;

                .top-crumb-left {
                    display: flex;
                    align-items: center;

                    .fold-menu {
                        cursor: pointer;
                    }

                    .bread {
                        margin-left: 10px;
                    }
                }

                .user-info {
                    .notice {
                        margin-right: 15px;
                        line-height: 25px;
                    }

                    .username-span {
                        cursor: pointer;
                        color: #4091ff;
                        line-height: 45px;
                        margin-right: 15px;
                        outline: unset;
                    }
                }
            }

            .wrapper {
                background: #eef0f3;
                padding: 20px;
                height: calc(100vh - 50px);

                .main {
                    background-color: #fff;
                    height: 100%;
                }
            }
        }
    }
</style>